Class Creation ----------------- $a = new class_db(); for default db connection $a = new class_db($db_uname, $db_pword, $db_name[, $db_host]); for specifying connection info. leave $db_host out if its localhost Class Attributes ----------------- debug_mode: true|false outputs sql querys to the browser #DATABASE FUNCTIONS GENERAL FUNCTIONS ----------------- get2DArrayNames($table, $selectClause, $whereClause, $orderClause) get2DArraySQL($sqlQuery) get2DArray($table, $selectClause, $whereClause, $orderClause) get1DArray($table, $selectField, $whereClause, $orderField) getNamedArray($table, $selectClause, $whereClause, $orderClause) countSql($sql) countItems($fromTable, $whereFieldName, $whereClause) sumItems($fromTable, $whereFieldName, $whereClause) maxItems($fromTable, $whereFieldName, $whereClause) avgItems($fromTable, $whereFieldName, $whereClause) getFirstItem($sql) getItem($fromTable, $getField, $whereClause) getTables($search = NULL) getTableColumns($table, $type = '1D') tableExists($table) exeSQL($sql) exeMultiSQL($sql) update($sql) updateItem($fromTable, $setClause, $whereClause) replace($sql) replaceItem($table, $fields, $values) insert($sql) insertItem($table, $fields, $values) deleteItems($fromTable, $whereClause) deleteTableData($table) DEBUG FUNCTIONS --------------- getLastSql() errorReport($sql) print2DArray($array) HISTORY --------------- 2013-04-11 BL Updated mysqli connection. Also modified it so it will not reconnect to a database if already connected $GLOBALS['DB_SELECTED'] 2012-04-03 BL Added Mysqli Support 2011-09-15 BL Added ODBC support and modified the _returnResult() to be better a bit 2010-10-28 BL Added limited support for sqlsrv connection (http://msdn.microsoft.com/en-US/library/cc296152%28v=SQL.90%29.aspx) Added $db_type=null to end of construct 2010-10-05 BL Made all whereclauses and orderclauses optional 2010-09-01 BL Added lockTable($table) and unLockTables() functions 2009-12-01 BL Removed __destruct as it was causing login problems on godaddy servers 2009-02-13 BL Condensed all functions to use _returnResult. Added better error handling and error page. 2008-11-14 BL Added back in simple check to see if loq_sql_errors exists before attempting and insert 2008-11-08 BL Added where clause validation against SQL injection. Emailes are sent regardless when an attempt is found 2008-09-11 BL Added new functions get2DArraySQL($sqlQuery), getFirstItem($sql), countSql($sql), update($sql), replace($sql), insert($sql), getLastSql() Added support to control error settings from config.inc.php DB_ERROR_PRINT, DB_ERROR_EMAIL, DB_ERROR_DB, DB_ERROR_EMAIL_TO, DB_ERROR_EMAIL_FROM 2008-08-11 BL Added new function getTableColumns. 2008-07-11 BL Made it so no errors are emailed when connection to DB down 2008-03-29 JF Added a function to check if a table exists. returns true/false with both MySQL and MS SQL support 2008-02-14 BL Added call to db_connect to the beginning of ALL functions. This fixes a bug when connecting to more than 1 DB at a time 2008-01-22 BL Added mssql support */ class D3corp_Db { # GENERAL VARS private $site_name = ''; # DB CONNECTION VARS protected $db_uname = ''; protected $db_pword = ''; protected $db_name = ''; protected $db_host = ''; public $db_type = 'mysql'; # mysql or mssql or sqlsrv protected $db_link; public $connected = false; private $die_on_fail_connection; // Set to false for alternate connections that the site doesn'te depend on private $db_cache = false; private $calls_made = 0; private $queries = array(); # DEBUG MODE public $debug_mode = false; # ERROR REPORTING public $error_print = false; # Print sql errors to browser public $error_email = true; # Email sql errors to public $error_db = true; # Insert error into database public $error_email_to = "briand@d3corp.com"; public $error_email_from = "briand@d3corp.com"; public $error_override_email_to = "briand@d3corp.com"; # Sends emails that are overridden to this as well private $from_log_error = false; private $sqli_keywords = array('concat','union select'); function __construct($db_uname=null, $db_pword=null, $db_name=null, $db_host=null, $die_on_fail_connection=true, $db_type=null) { $this->_getMagentoConfig(); if ($db_uname && $db_pword && $db_name) { $this->db_uname = $db_uname; $this->db_pword = $db_pword; $this->db_name = $db_name; if ($db_host) $this->db_host = $db_host; if ($db_type) $this->db_type = $db_type; } $this->die_on_fail_connection = $die_on_fail_connection; if (defined('DB_ERROR_PRINT')) $this->error_print = DB_ERROR_PRINT; if (defined('DB_ERROR_EMAIL')) $this->error_email = DB_ERROR_EMAIL; if (defined('DB_ERROR_DB')) $this->error_db = DB_ERROR_DB; if (defined('DB_ERROR_EMAIL_TO')) { $this->error_email_to = DB_ERROR_EMAIL_TO; $this->error_email_from = DB_ERROR_EMAIL_FROM; } if ($this->debug_mode == true) echo "{$this->db_name} - {$this->db_uname} - {$this->db_pword} - {$this->db_host}
"; #CONNECT TO DATABASE $this->db_connect($this->db_uname, $this->db_pword, $this->db_name, $this->db_host); } private function _getMagentoConfig(){ $config = Mage::getConfig()->getResourceConnectionConfig("default_setup"); $this->db_host = (string)$config->host; $this->db_uname = (string)$config->username; $this->db_pword = (string)$config->password; $this->db_name = (string)$config->dbname; } public function setDBCache($setto) { $this->db_cache = ($setto == false) ? false : true; } private function _incCalls() { //array_push($this->queries, $this->getLastSql()); $this->calls_made++; } public function getCalls() { return $this->calls_made; } public function getSelectedDB() { $db_selected = false; if (isset($GLOBALS['DB_SELECTED'])) { $db_selected = $GLOBALS['DB_SELECTED']; } return $db_selected; } function db_connect($db_uname, $db_pword, $db_name, $db_host) { if ($this->getSelectedDB() == $db_name) return; switch ($this->db_type) { case 'mysql': if (!$this->db_link = @mysql_connect($db_host,$db_uname,$db_pword)) { if ($this->die_on_fail_connection == true) { $this->errorReport('', 'Connection Failed', true, false); } return false; } else { //mysql_set_charset('utf8', $this->db_link); if (!@mysql_select_db($db_name, $this->db_link)) { $this->errorReport('', @mysql_error(), true, false); return false; } else { $this->connected = true; return true; } } break; case 'mysqli': $this->db_link = @new mysqli($db_host,$db_uname,$db_pword, $db_name); if ($this->db_link->connect_error) { if ($this->die_on_fail_connection == true) { $this->errorReport('', 'Connection Failed '.$this->db_link->connect_error, true, false); } return false; } else { //$this->db_link->set_charset("utf8"); if (!@mysqli_select_db($this->db_link, $db_name)) { $this->errorReport('', mysqli_error($this->db_link), true, false); return false; } else { $GLOBALS['DB_SELECTED'] = $db_name; $this->connected = true; return true; } } break; case 'mssql': if (!$this->db_link = @mssql_connect($db_host,$db_uname,$db_pword, true)) { if ($this->die_on_fail_connection == true) { $this->errorReport('', 'Connection Failed', true, false); } return false; } else { if (!@mssql_select_db($db_name, $this->db_link)) { $this->errorReport('', @mssql_get_last_message(), true, false); return false; } else { $this->connected = true; return true; } } break; case 'sqlsrv': $connectionInfo = array("Database"=>$db_name, "UID" => $db_uname, "PWD" => $db_pword); $host = ($db_host == 'localhost') ? '(local)' : $db_host; $this->db_link = sqlsrv_connect($host, $connectionInfo); if (!$this->db_link) { if ($this->die_on_fail_connection == true) { $this->errorReport('', 'Connection Failed', true, false); } return false; } else { $this->connected = true; return true; } break; case 'odbc': if (!$this->db_link = @odbc_connect($db_name,$db_uname,$db_pword)) { if ($this->die_on_fail_connection == true) { $this->errorReport('', 'Connection Failed:'.odbc_errormsg(), true, false); } return false; } else { $this->connected = true; return true; } break; } } private function validateWhere($whereClause,$sqlQuery) { $return = true; foreach ($this->sqli_keywords as $key) { if (is_numeric(strpos(strtolower($whereClause), $key))) { $return = false; } } if ($return == false) { $this->errorReport($sqlQuery, "SQL INJECTION ATTEMPT: " . $sqlQuery, $die = false, $email = true, $override_email = true); } return $return; } public function lockTable($table, $lock_type='READ') { $lock_type = strtoupper($lock_type); $lock_types = array('READ', 'READ LOCAL', 'WRITE'); if (!in_array($lock_type, $lock_types)) return false; $sql = 'LOCK TABLE '.addslashes_auto($table).' '.$lock_type; $sqlResult = $this->_returnResult($sql,NULL,NULL,true); return true; } public function unLockTables() { $sql = 'UNLOCK TABLES'; $sqlResult = $this->_returnResult($sql,NULL,NULL,true); } public function escape($string) { switch ($this->db_type) { case 'mysql': return mysql_real_escape_string($string); break; case 'mysqli': return mysqli_real_escape_string($this->db_link, $string); break; case 'mssql': return str_replace("'", "''", $string); break; default: return addslashes($string); break; } } private function _returnResult($sql, $type = 'NULL', $fetch_type = 'index', $exec_only = false) { $this->db_connect($this->db_uname, $this->db_pword, $this->db_name, $this->db_host); $this->last_sql = $sql; if ($this->debug_mode == true) echo $sql."
"; switch ($this->db_type) { case 'mysql': $queryFunc = 'mysql_query'; $errorFunc = 'mysql_error'; $numRowsFunc = 'mysql_num_rows'; if ($fetch_type == 'index') { $fetchFunc = 'mysql_fetch_array'; } else { $fetchFunc = 'mysql_fetch_assoc'; } // Override fetch functions here /* switch ($type) { case 'XXX': mysql_fetch_assoc break; } */ break; case 'mysqli': $queryFunc = 'mysqli_query'; $errorFunc = 'mysqli_error'; $numRowsFunc = 'mysqli_num_rows'; if ($fetch_type == 'index') { $fetchFunc = 'mysqli_fetch_array'; } else { $fetchFunc = 'mysqli_fetch_assoc'; } // EXECUTE if (!$sqlResult = $queryFunc($this->db_link, $sql)) { if ($this->from_log_error == false) { $this->errorReport($sql, $errorFunc($this->db_link)); } else { $this->from_log_error = false; } } break; case 'mssql': $queryFunc = 'mssql_query'; $errorFunc = 'mssql_get_last_message'; $numRowsFunc = 'mssql_num_rows'; if ($fetch_type == 'index') { $fetchFunc = 'mssql_fetch_array'; } else { $fetchFunc = 'mssql_fetch_assoc'; } // Override fetch functions here /* switch ($type) { case 'XXX': break; } */ break; case 'sqlsrv': $queryFunc = 'sqlsrv_query'; $errorFunc = 'sqlsrv_get_last_message'; $numRowsFunc = 'sqlsrv_num_rows'; // sqlsrv only has this function and by default return both numeric and assoc keys $fetchFunc = 'sqlsrv_fetch_array'; // EXECUTE if (!$sqlResult = $queryFunc($this->db_link, $sql, array(), array( "Scrollable" => 'static' ) )) { if ($this->from_log_error == false) { $this->errorReport($sql, sqlsrv_errors()); } else { $this->from_log_error = false; } } break; case 'odbc': $queryFunc = 'odbc_exec'; $errorFunc = 'odbc_errormsg'; $numRowsFunc = 'odbc_num_rows'; if ($fetch_type == 'index') { $fetchFunc = 'odbc_fetch_array'; } else { $fetchFunc = 'odbc_fetch_array'; } // EXECUTE if (!$sqlResult=$queryFunc($this->db_link,$sql)) { if ($this->from_log_error == false) { $this->errorReport($sql, $errorFunc()); } else { $this->from_log_error = false; } } break; } // sqlsrv/odbc are different thatn the other connections if (!in_array($this->db_type, array('sqlsrv', 'odbc', 'mysqli'))) { if (!$sqlResult = @$queryFunc($sql, $this->db_link)) { if ($this->from_log_error == false) { $this->errorReport($sql, $errorFunc()); } else { $this->from_log_error = false; } } } if ($exec_only == true) { return $sqlResult; } if ($sqlResult) { if ($type == 'insert') { switch ($this->db_type) { case 'mysqli': return mysqli_insert_id($this->db_link); break; case 'mysql': return mysql_insert_id(); break; case 'mssql': if ($row = mssql_fetch_row($sqlResult)) { $id = trim($row[0]); } return $id; break; } } //$numResults = $numRowsFunc($sqlResult); //if ($numResults > 0) { switch ($type) { case 'firstitem': $sqlRow = $fetchFunc($sqlResult); return $sqlRow[0]; break; case 'firstrow': //if ($numRowsFunc($sqlResult) > 0) { // return $fetchFunc($sqlResult); //} else { // return false; //} $tmparray = $fetchFunc($sqlResult); return (count($tmparray) > 0) ? $tmparray : false; break; case 'firstcol': /* for ($i=0; $i < $numResults; $i++) { $row = $fetchFunc($sqlResult); $tmparray[$i] = $row[0]; } return $tmparray; */ $tmparray = array(); while ($row = $fetchFunc($sqlResult)) { //$tmparray[] = $row[0]; $tmparray[] = current($row); } return (count($tmparray) > 0) ? $tmparray : false; break; case 'fullarray': /* for ($i=0; $i < $numResults; $i++) { $row = $fetchFunc($sqlResult); $tmparray[$i] = $row; } return $tmparray; */ $tmparray = array(); while ($row = $fetchFunc($sqlResult)) { $tmparray[] = $row; } return (count($tmparray) > 0) ? $tmparray : false; break; default: return $sqlResult; break; } //} } return false; } # DB FUNCTIONS: GENERAL public function get2DArrayNames($table, $selectClause, $whereClause=NULL, $orderClause=NULL, $limit=NULL) { $sql = "SELECT $selectClause FROM $table"; if ($whereClause && $whereClause <> 'all') $sql .= " WHERE $whereClause"; if ($orderClause) $sql .= " ORDER BY $orderClause"; if ($limit) $sql .= " LIMIT $limit"; if ($this->validateWhere($whereClause,$sql)) { return $this->_returnResult($sql, 'fullarray', 'names'); } else { return false; } } public function get2DArraySQL($sql) { return $this->_returnResult($sql, 'fullarray', 'names'); } public function get2DArray($table, $selectClause, $whereClause=NULL, $orderClause=NULL, $limit=NULL) { $sql = "SELECT $selectClause FROM $table"; if ($whereClause && $whereClause <> 'all') $sql .= " WHERE $whereClause"; if ($orderClause) $sql .= " ORDER BY $orderClause"; if ($limit) $sql .= " LIMIT $limit"; if ($this->validateWhere($whereClause,$sql)) { return $this->_returnResult($sql, 'fullarray', 'index'); } else { return false; } } public function get1DArray($table, $selectClause, $whereClause=NULL, $orderClause=NULL, $limit=NULL) { $sql = "SELECT $selectClause FROM $table"; if ($whereClause && $whereClause <> 'all') $sql .= " WHERE $whereClause"; if ($orderClause) $sql .= " ORDER BY $orderClause"; if ($limit) $sql .= " LIMIT $limit"; if ($this->validateWhere($whereClause,$sql)) { return $this->_returnResult($sql, 'firstcol', 'index'); } else { return false; } } public function getNamedArray($table, $selectClause, $whereClause=NULL, $orderClause=NULL) { $sql = "SELECT $selectClause FROM $table"; if ($whereClause && $whereClause <> 'all') $sql .= " WHERE $whereClause"; if ($orderClause) $sql .= " ORDER BY $orderClause"; if ($this->validateWhere($whereClause,$sql)) { return $this->_returnResult($sql, 'firstrow', 'names'); } else { return false; } } public function getNamedArraySql($sql) { return $this->_returnResult($sql, 'firstrow', 'names'); } public function countItems($fromTable, $whereFieldName, $whereClause=NULL) { $sql = "SELECT COUNT($whereFieldName) AS counted FROM $fromTable"; if ($whereClause && $whereClause <> 'all') $sql .= " WHERE $whereClause"; if ($this->validateWhere($whereClause,$sql)) { return $this->_returnResult($sql, 'firstitem', 'index'); } else { return 0; } } public function sumItems($fromTable, $whereFieldName, $whereClause=NULL) { $sql = "SELECT SUM($whereFieldName) AS maxed FROM $fromTable"; if ($whereClause && $whereClause <> 'all') $sql .= " WHERE $whereClause"; if ($this->validateWhere($whereClause,$sql)) { return $this->_returnResult($sql, 'firstitem', 'index'); } else { return false; } } public function maxItems($fromTable, $whereFieldName, $whereClause=NULL) { $sql = "SELECT MAX($whereFieldName) AS maxed FROM $fromTable"; if ($whereClause && $whereClause <> 'all') $sql .= " WHERE $whereClause"; if ($this->validateWhere($whereClause,$sql)) { return $this->_returnResult($sql, 'firstitem', 'index'); } else { return false; } } public function avgItems($fromTable, $whereFieldName, $whereClause=NULL) { $sql = "SELECT AVG($whereFieldName) AS maxed FROM $fromTable"; if ($whereClause && $whereClause <> 'all') $sql .= " WHERE $whereClause"; if ($this->validateWhere($whereClause,$sql)) { return $this->_returnResult($sql, 'firstitem', 'index'); } else { return false; } } public function getItem($fromTable, $getField, $whereClause=NULL) { $sql = "SELECT $getField FROM $fromTable"; if ($whereClause && $whereClause <> 'all') $sql .= " WHERE $whereClause"; if ($this->validateWhere($whereClause,$sql)) { return $this->_returnResult($sql, 'firstitem', 'index'); } else { return false; } } function getTables($search = NULL) { switch ($this->db_type) { case 'mysqli': case 'mysql': if ($search) $sql = "SHOW TABLES LIKE '$search'"; else $sql = "SHOW TABLES"; return $this->_returnResult($sql, 'firstcol', 'index'); break; case 'mssql': if ($search) $sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_NAME = '$search'"; else $sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE = 'BASE TABLE'"; return $this->_returnResult($sql, 'fullarray', 'index'); break; } return false; } function getTableColumns($table, $type = '1D') { $this->db_connect($this->db_uname, $this->db_pword, $this->db_name, $this->db_host); switch ($this->db_type) { case 'mysqli': $sqlQuery = "SHOW COLUMNS FROM $table"; $this->last_sql = $sqlQuery; if ($this->debug_mode == true) echo $sqlQuery."
"; $sqlResult = mysqli_query($this->db_link, $sqlQuery) or $this->errorReport($sqlQuery, mysql_error()); $numResults = mysqli_num_rows($sqlResult); if ($numResults > 0) { $inc = 0; while ($row = mysqli_fetch_assoc($sqlResult)) { if ($type == '1D') $tmparray[$inc] = $row['Field']; else $tmparray[$inc] = $row; $inc++; } } else { $tmparray = false; } break; case 'mysql': $sqlQuery = "SHOW COLUMNS FROM $table"; $this->last_sql = $sqlQuery; if ($this->debug_mode == true) echo $sqlQuery."
"; $sqlResult = mysql_query($sqlQuery, $this->db_link) or $this->errorReport($sqlQuery, mysql_error()); $numResults = mysql_num_rows($sqlResult); if ($numResults > 0) { $inc = 0; while ($row = mysql_fetch_assoc($sqlResult)) { if ($type == '1D') $tmparray[$inc] = $row['Field']; else $tmparray[$inc] = $row; $inc++; } } else { $tmparray = false; } break; case 'mssql': $sqlQuery = "SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE (TABLE_NAME = '$table') AND (TABLE_CATALOG = '".$this->db_name."')"; $this->last_sql = $sqlQuery; if ($this->debug_mode == true) echo $sqlQuery."
"; $sqlResult = mssql_query($sqlQuery, $this->db_link) or $this->errorReport($sqlQuery, mssql_get_last_message()); $numResults = mssql_num_rows($sqlResult); if ($numResults > 0) { $inc = 0; while ($row = mysql_fetch_assoc($sqlResult)) { if ($type == '1D') $tmparray[$inc] = $row['COLUMN_NAME']; else $tmparray[$inc] = $row; $inc++; } } else { $tmparray = false; } break; } return $tmparray; } function tableExists($table) { switch ($this->db_type) { case 'mysqli': $sql = "SHOW TABLE STATUS LIKE '$table'"; $sqlResult = $this->_returnResult($sql); return (@mysqli_num_rows($sqlResult) == 1); break; case 'mysql': $sql = "SHOW TABLE STATUS LIKE '$table'"; $sqlResult = $this->_returnResult($sql); return (@mysql_num_rows($sqlResult) == 1); break; case 'mssql': $sql = "SELECT TABLE_NAME FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_TYPE='BASE TABLE' AND TABLE_NAME='".$table."'"; $sqlResult = $this->_returnResult($sql); return (@mssql_num_rows($sqlResult) == 1); break; } return false; } function exeSQL($sql) { $sqlResult = $this->_returnResult($sql,NULL,NULL,true); return true; } function exeMultiSQL($sql, $pattern=";") { $sql_queries = split($pattern, $sql); if (is_array($sql_queries)) { for ($i=0; $i < count($sql_queries); $i++) { if (trim($sql_queries[$i])) { $sqlResult = $this->_returnResult($sql_queries[$i],NULL,NULL,true); } } } return true; } function update($sql) { $sqlResult = $this->_returnResult($sql,NULL,NULL,true); return true; } function updateItem($fromTable, $setClause, $whereClause) { if ($whereClause == 'all') $sqlQuery = "UPDATE $fromTable SET $setClause"; else $sqlQuery = "UPDATE $fromTable SET $setClause WHERE $whereClause"; if ($this->validateWhere($whereClause,$sqlQuery)) { return $this->update($sqlQuery); } else { return false; } } public function replace($sql) { $this->db_connect($this->db_uname, $this->db_pword, $this->db_name, $this->db_host); $this->last_sql = $sql; return $this->insert($sql); } public function replaceItem($table, $fields, $values) { # REPLACE IS NOT COMPATIBLE WITH MS SQL 2000 $sql = "REPLACE INTO $table ($fields) VALUES ($values)"; return $this->replace($sql); } public function insert($sql) { if ($this->db_type == 'mssql') { $sql = $sql . "; SELECT @@identity AS id"; } return $this->_returnResult($sql, 'insert'); } public function insertItem($table, $fields, $values) { $sql = "INSERT INTO $table ($fields) VALUES ($values)"; return $this->insert($sql); } public function deleteItems($fromTable, $whereClause) { $sql = "DELETE FROM $fromTable WHERE $whereClause"; if ($this->validateWhere($whereClause,$sql)) { $sqlResult = $this->_returnResult($sql,NULL,NULL,true); return true; } else { return false; } } function deleteTableData($table) { $this->db_connect($this->db_uname, $this->db_pword, $this->db_name, $this->db_host); $sql = "TRUNCATE TABLE $table"; $this->last_sql = $sql; if ($this->debug_mode == true) echo $sql."
"; switch ($this->db_type) { case 'mysqli': $sqlResult = mysqli_query($this->db_link, $sql) or $this->errorReport($sql, mysqli_error()); break; case 'mysql': $sqlResult = mysql_query($sql, $this->db_link) or $this->errorReport($sql, mysql_error()); break; case 'mssql': $sqlResult = mssql_query($sql, $this->db_link) or $this->errorReport($sql, mssql_get_last_message()); $sqlResult = mssql_query("DBCC CHECKIDENT ($table, RESEED, 1)", $this->db_link) or $this->errorReport($sql, mssql_get_last_message()); break; } return true; } # DB FUNCTIONS: DEBUG function getLastSql() { return $this->last_sql; } function errorReport($sql, $error, $die = false, $email = true, $override_email = false) { # PRINT if ($this->error_print == true) { if ($_SERVER['SERVER_SOFTWARE']) { $error_table = "
SQL ".displayHTML($sql)."
Error $error
Date ".date('Y/m/d H:i:s')."
"; } else { $error_table = "\nSQL:$sql\nERROR:$error\n"; } echo $error_table; } # EMAIL if (($this->error_email == true && $email == true) || $override_email == true) { $error_email = "

An SQL Error has occured


SQL ".$sql."
Error $error
Date ".date('Y/m/d H:i:s')."
URL getCurrentURL()."\">".$this->getCurrentURL()."
Remote User IP/Hostname/Agent
".$_SERVER['REMOTE_ADDR']."
".@gethostbyaddr($_SERVER['REMOTE_ADDR'])."
".$_SERVER['HTTP_USER_AGENT']."
File ".$_SERVER['SCRIPT_FILENAME']."
Referer ".$_SERVER['HTTP_REFERER']."
Post Data ".$this->print_r_html($_POST)."
"; echo 'error'; print_r($error); echo $sql; die(); if ($override_email == true) { send_email( $from_email = $this->error_email_from, $from_name, $to_email = $this->error_override_email_to, $to_name, $message = $error_email, $subject = $this->site_name." SQL Error", $is_html = true ); //sendEmail($this->error_override_email_to, $this->error_email_from, $error_email, $this->site_name." SQL Error", true); } send_email( $from_email = $this->error_email_from, $from_name, $to_email = $this->error_email_to, $to_name, $message = $error_email, $subject = $this->site_name." SQL Error", $is_html = true ); //sendEmail($this->error_email_to, $this->error_email_from, $error_email, $this->site_name." SQL Error", true); } if ($die == true) { if (file_exists(DOC_ROOT.SUB_FOLDER.'/app/design/templates/_error.php')) { include(DOC_ROOT.SUB_FOLDER.'/app/design/templates/_error.php'); } else { echo 'An error has occured'; } die; } # INSERT INTO DB date('Y-m-d H:i:s') if ($this->error_db == true && $this->getTables('log_sql_errors')) { $this->from_log_error = true; $this->insertItem('log_sql_errors', 'query, sql_error, error_date, page, file_location, remote_ip, remote_host, remote_agent, referer, post_data, back_trace', "'".addslashes_auto($sql)."', '".addslashes_auto($error)."', '".date('Y-m-d H:i:s')."', '".addslashes_auto($this->getCurrentURL())."', '{$_SERVER['SCRIPT_FILENAME']}', '{$_SERVER['REMOTE_ADDR']}', '".@gethostbyaddr($_SERVER['REMOTE_ADDR'])."', '".addslashes_auto($_SERVER['HTTP_USER_AGENT'])."', '".addslashes_auto($_SERVER['HTTP_REFERER'])."', '".addslashes_auto($this->print_r_html($_POST))."','".addslashes_auto(getDebugBackTrace(false))."'"); } } function print2DArray($array) { if (is_array($array)) { $ret_data = ''; for ($i=0; $i < count($array); $i++) { $keys = array_keys($array[$i]); if ($i == 0) { $ret_data .= ''; for ($j=0; $j < count($array[$i]); $j++) { $ret_data .= ""; } $ret_data .= ''; } if ($i % 2 == 0) $bgcolor = '#FFFFFF'; else $bgcolor = '#DDDDDD'; $ret_data .= ""; for ($j=0; $j < count($array[$i]); $j++) { $ret_data .= ""; } $ret_data .= ''; } $ret_data .= '
{$keys[$j]}
{$array[$i][$keys[$j]]}
'; return $ret_data; } else { return 'Not an array'; } } function getCurrentURL($short = false) { if ($short == false) { if ($_SERVER['SERVER_PORT'] == '443') $url = 'https://'; else $url = 'http://'; $url .= $_SERVER['HTTP_HOST']; } if (isset($GLOBALS['SITE_GLOBALS']['full_url_qs'])) { $url .= $GLOBALS['SITE_GLOBALS']['full_url_qs']; } else { $url .= $_SERVER['REQUEST_URI']; } return $url; } function print_r_html($array) { if (is_array($array)) { if (count($array) > 0) { return str_replace('', '
', str_replace(' ', ' ', print_r($array,true))); } else { return false; } } else { return false; } } public function convertArray($array, $return_type) { if ($return_type == 'insert') { $fields = ''; $values = ''; foreach ($array as $field=>$value) { if ($fields != '') { $fields .= ','; $values .= ','; } $fields .= "`".addslashes_auto($field)."`"; $values .= "'".addslashes_auto($value)."'"; } return array($fields, $values); } else { $update = ''; foreach ($array as $field=>$value) { if ($update != '') { $update .= ','; } $update .= "`".addslashes_auto($field)."`='".addslashes_auto($value)."'"; } return $update; } } } ?>